<template>
	<view class="container">
		<input v-model="userName" class="input" placeholder="请输入用户名"></input>
		<input v-model="firstPassword" password class="input" placeholder="请输入密码"></input>
		<input v-model="secondPassword" password  class="input" placeholder="请再次输入密码"></input>
		<button @click="onRegisterButtonClicked()" class="button">注册</button>
	</view>
</template>

<script>
	let tag = 'register';
	export default {
		data() {
			return {
				//用户名
				userName: "",
				//第一次输入的密码
				firstPassword: "",
				//第二次输入的密码
				secondPassword: "",
			}
		},
		methods: {
			onRegisterButtonClicked() {
				this.$log.info(tag, "onRegisterButtonClicked");
				//检查用户名和密码是否为空
				if (this.userName && this.userName.length > 0
					&& this.firstPassword && this.firstPassword.length > 0
					&& this.secondPassword && this.secondPassword.length > 0
				) {
					this.$log.info(tag, "onRegisterButtonClicked null check passed");
				} else {
					this.$log.error(tag, "onRegisterButtonClicked something empty");
					uni.showToast({
						icon:'none',
						title:"用户名或密码为空",
						duration:1500,
					});
					return;
				}
				//检查密码是否一致
				if (this.firstPassword !== this.secondPassword) {
					this.$log.error(tag, "onRegisterButtonClicked different passwords");
					uni.showToast({
						icon:'error',
						title:"密码不一致",
						duration:1500,
					});
					//清空密码
					this.firstPassword = "";
					this.secondPassword = "";
					return;
				}
				// 开始注册流程
				this.getCode(this.userName, this.firstPassword);
				
			},
			
			getCode(userName, password){
				this.$log.info(tag, "getCode");
				uni.request({
					url:  `${this.constant.BASE_URL}/api/code`,
					method:'GET',
					data:{
						password: password,
						username: userName
					},
					success: (res) => {
						this.$log.info(tag, "getCode success")
						this.$log.info(tag, JSON.stringify(res.data))
						this.$log.info(tag, res.data.valid_code)
						this.register(userName,password,res.data.valid_code)
					},
					fail: (err) => {
						this.$log.error(tag, err.errMsg)
					}
					
				});
				
			},
			register(userName, password, valid_code){
				this.$log.info(tag, "register")
				uni.request({
					url: `${this.constant.BASE_URL}/api/register`,
					method:'POST',
					data:{
						password: password,
						username: userName,
						code: valid_code
					},
					success: (res) => {
						this.$log.info(tag, "register success")
						this.$log.info(tag, JSON.stringify(res.data))
						if(res.data.code == 200){
							this.$log.info(tag, `${userName}注册成功`)
							uni.showToast({
								icon:'success',
								title: res.data.msg
							})
							setTimeout(()=>{
								uni.navigateBack();
							})
							
						}
						else{
							this.$log.error(tag, `${userName}注册失败`)
							uni.showToast({
								icon:'error',
								title: res.data.msg
							})
						}
					},
					fail: (err) => {
						this.$log.error(tag, err.errMsg)
					}
				})
			}
			
		}
	}
</script>

<style>
	.container {
		padding: 20px 20px;
		height: 50vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.input {
		width: 100%;
		border-style: solid;
		border-color: grey;
		border-radius: 20px;
		border-width: 1px;
		padding: 5px 10px;
		font-size: 35rpx;
		margin-bottom: 10px;
	}
	
	.button {
		width: 100%;
		background-color: #08f;
		font-size: 35rpx;
		color: white;
		font-weight: bold;
		border-radius: 20px;
		margin-bottom: 10px;
	}
</style>
